// TOWN SCRIPT
//    Town 0: Llylgamyn

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

// flags:
// 0, 0 = training on
// 0, 1 = healing herbs (1)
// 0, 2 = spiritual herbs (1)
// 0, 3 = energetic herbs (1)
// 0, 4 = spiritual herbs (2)
// 0, 5 = energetic herbs (2)
// 0, 6 = healing herbs (2)
// 0, 7 = toadstools
// 0, 8 = mandrake
// 0, 9 = graymold
// 0, 10 = paid for training
// 0, 11 = Gilgamesh conversation flag
// 0, 12 = June conversation flag
// 0, 13 = August conversation flag
// 0, 14 = Wanderer conversation flag
// 0, 15 = Trebor conversation flag
// 0, 18 = Am conversation flag

begintownscript;

variables;

short i, dd, choice;

body;

beginstate INIT_STATE;
	turn_off_training(1);
	set_crime_tolerance(3);

	set_name(12, "Personal Guard");
	set_name(13, "Personal Guard");
	set_name(17, "Trebor the Overlord");
	set_char_dialogue_pic(17, 520, 0);
	set_name(19, "Boltac");
	set_char_dialogue_pic(19, 521, 0);
	set_name(20, "Son of Boltac");
	set_char_dialogue_pic(20, 522, 0);
	set_name(21, "Gilgamesh");
	set_char_dialogue_pic(21, 1945, 0);
	set_name(22, "June");
	set_char_dialogue_pic(22, 523, 0);
	set_name(23, "May");
	set_char_dialogue_pic(23, 524, 0);
	set_name(24, "August");
	set_char_dialogue_pic(24, 1960, 0);
	set_name(25, "Wanderer");
	set_char_dialogue_pic(25, 1962, 0);
	set_name(31, "Am");
	set_char_dialogue_pic(31, 1961, 0);

	// wanderer here (only every other day...)
	// am here (only on opposite days...)
	dd = what_day_of_scenario() % 2;
	if (dd == 0) {
		erase_char(25);
	}
	else {
		erase_char(31);
	}

	// reset flag
	set_flag(0, 16, 0);
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
	if (get_ran(1, 0, 100) < 5)
		text_bubble_on_char(12, "You must have permission to enter");
	if (get_ran(1, 0, 100) < 5)
		text_bubble_on_char(13, "No entry to Overlord's quarters");
	if (get_ran(1, 0, 100) < 5)
		text_bubble_on_char(16, "Prove yourselves to the Overlord");
	if (get_ran(1, 0, 100) < 5) {
		if (get_flag(50, 1) > 0) {
			if (get_flag(50, 3) > 0)
				text_bubble_on_char(17, "My precious amulet is mine!");
			else
				text_bubble_on_char(17, "Give me my amulet");
		}
		else {
			text_bubble_on_char(17, "Find my amulet");
		}
	}
	if (get_ran(1, 0, 100) < 5) {
		if (get_flag(50, 1) == 0) {
			i = get_ran(1, 1, 100);
			if (i <= 33)
				text_bubble_on_char(18, "Oh dear!");
			else if ((i > 33) && (i <= 66))
				text_bubble_on_char(18, "We've lost the amulet");
			else
				text_bubble_on_char(18, "I can't see where it is");
		}
		else {
			text_bubble_on_char(18, "The amulet has been found");
		}
	}
break;

beginstate 10;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a ladder leading down into darkness here.", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Climb down");
	choice = run_dialog(0);
	if (choice == 2) {
		// ladder down to first level of proving grounds...
		move_to_new_town(1, 4, 42);
	}
break;

beginstate 11;
	if (get_flag(0, 10) == 0) {
		message_dialog("You must pay for training first.", "");
		block_entry(1);
	}
	else {
		block_entry(0);
		if (get_flag(0, 0) == 0) {
			set_flag(0, 0, 1);
			turn_off_training(0);
		}
	}
break;

beginstate 12;
	if (get_flag(0, 0) > 0) {
		set_flag(0, 0, 0);
		turn_off_training(1);
	}
break;

beginstate 13;
	set_flag(0, 10, 0); // reset when you leave...
break;

beginstate 14;
	if (get_flag(50, 0) == 0) {
		block_entry(1);
		play_sound(18);
		message_dialog("You hear the sound of steel as the guard pulls his weapons and blocks your path.", "_You must have permission from the Overlord to enter_ he says gruffly.");
	}
	else {
		block_entry(0);
		if ((get_flag(0, 17) == 0)  && (get_flag(50, 2) == 0)) {
			set_flag(0, 17, 1);
			message_dialog("The guard recognizes you just as he starts to move to stop you.", "He salutes as you pass him by _Good luck._");
		}
	}
break;

beginstate 15;
	message_dialog("The bookshelf is filled with journals full of incoherent ramblings.", "It appears the Overlord found a pencil...");
break;

beginstate 16;
	message_dialog("The bookshelf is full of rambling journals of the Overlord.", "One of the passages you can interpret says 'Werdna is a weenie'.");
break;

beginstate 17;
	if (get_flag(50, 2) == 0) {
		if (get_ran(1, 1, 100) > 75) {
			set_flag(0, 16, 1);
			message_dialog("As the door opens, you see the guard grabbing his sword.", "You can hear him growling at you...");
		}
		else {
			message_dialog("The guard doesn't seem to notice that you've open the door.", "Perhaps you can sneak in and grab a quick look.");
		}
	}
break;

beginstate 18;
	if (get_flag(50, 2) > 0) {
		set_flag(0, 16, 0);
	}
	if (get_flag(0, 16) > 0) {
		if (get_ran(1, 1, 100) > 75) {
			set_flag(0, 16, 0);
			message_dialog("A sudden noise has distracted the guard and he fails to see you slip in the bedroom.", "What a fortunate turn of events.");
		}
		else {
			message_dialog("The guard sounds an alarm as he chases after you.", "You have a feeling you shouldn't have done this.");
			make_town_hostile();
		}
	}
break;

beginstate 19;
	message_dialog("A small brass plaque on the base of the statue reads:", "Trebor, hero and saviour - long live Trebor.");
break;

beginstate 20;
	set_flag(0, 17, 0);
break;
